package com.xzx.refactoring;

import java.util.Enumeration;
import java.util.Vector;

/**
 * 顾客
 *
 * @author xinzhixuan
 * @version V1.0
 * @date 2017/8/27 20:26
 */
public class Customer {
    private String name;
    private Vector rentals = new Vector();

    public Customer(String name) {
        this.name = name;
    }


    public void addRental(Rental arg) {
        rentals.addElement(arg);
    }

    /**
     * 生成详单
     *
     * @return .
     */
    public String statement() {
        Enumeration _rentals = rentals.elements();
        String result = "Rental Record for " + getName() + "\n";
        while (_rentals.hasMoreElements()) {
            Rental each = (Rental) _rentals.nextElement();
            result += "\t" + each.getMovie().getTitle() + "\t" + each.getCharge() + "\n";
        }

        result += "Amount owed is " + getTotalCharge() + "\n";
        result += "You earned " + getTotalFrequentRenterPoints() + " frequent renter points";
        return result;
    }

    /**
     * 计算租金
     *
     * @return .
     */
    public double getTotalCharge() {
        double result = 0;
        Enumeration _rentals = rentals.elements();
        while (_rentals.hasMoreElements()) {
            Rental each = (Rental) _rentals.nextElement();
            result += each.getCharge();
        }
        return result;
    }

    public String getName() {
        return name;
    }

    /**
     * 计算返点积分
     *
     * @return
     */
    public int getTotalFrequentRenterPoints() {
        int result = 0;//积分点
        Enumeration _rentals = rentals.elements();
        while (_rentals.hasMoreElements()) {
            Rental each = (Rental) _rentals.nextElement();
            result += each.getFrequentRentalPoints();
        }
        return result;
    }

    public static void main(String[] args) {
        Customer customer = new Customer("张三");
        customer.addRental(new Rental(new Movie("定期", 0), 3));
        customer.addRental(new Rental(new Movie("孩子", 2), 3));
        customer.addRental(new Rental(new Movie("新片", 1), 3));

        System.out.println(customer.statement());
    }

}
